package com.sgb.contract.api;

import com.sgb.contract.BuildConfig;
import com.sgb.contract.utils.StringUtils;
import com.sgb.net.core.BaseCompatibleNet;
import com.swgk.core.util.MLog;
import com.swgk.core.util.SharedPreferenceUtil;

import okhttp3.OkHttpClient;

/**
 * 合同网络请求 网络请求 retrofit2 API
 *
 * @author cyj
 */
public class ContractNet extends BaseCompatibleNet {

    private static class Single {
        private static final ContractNet INSTANCE = new ContractNet();
    }

    public static ContractNet getInstance() {
        return Single.INSTANCE;
    }

    /**
     * 合同Api
     */
    private final ContractApi contractApi = getService(ContractApi.class, BuildConfig.BASE_URL_HT);
    /**
     * 资金中心Api
     */
    private final CapitalApi capitalApi = getService(CapitalApi.class, BuildConfig.BASE_URL_ZJ);

    /**
     * Builder 网络配置处理
     *
     * @param builder
     * @see OkHttpClient.Builder    所有的基于okhttp3网络配置属性可以查看这里
     */
    @Override
    protected void handleBuilder(OkHttpClient.Builder builder) {
        if(BuildConfig.IS_MODULE_DEBUG) {
            builder.addInterceptor(chain -> {
                String token = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.resourcePCToken, "");
                MLog.d("TAG", "Bearer " + token);
                return chain.proceed(chain.request().newBuilder()
                        .addHeader("Authorization", StringUtils.isEmpty(token) ? "Basic Z2N0eDpnY3R4" : String.format("Bearer %s", token))
                        .addHeader("platform", "android")
                        .addHeader("Content-Type", "application/x-www-form-urlencoded")
                        .addHeader("type", "app-api-java")
                        .build());
            });
        }
    }

    /**
     * 获取合同Api接口对象
     *
     * @return ContractApi
     */
    public ContractApi getContractApi() {
        return contractApi;
    }

    /**
     * 获取资金中心接口对象
     *
     * @return ContractApi
     */
    public CapitalApi getCapitalApi() {
        return capitalApi;
    }

}
